$(function() {
	$('ul.items-list li h4 a.company').bind('click', toggle);
	
	function toggle() {
		var desc = $(this).parents('li').find('.descripting');
		if (desc.is(':hidden'))
			desc.show();
		else
			desc.hide();
	}

	$('#add-item a').click(function() {
		$('#add-item').hide();
		$('ul.items-list li.default').show();
	});
	
	$('.default .b-ctrl .cancel').click(function() {
		$('#add-item').show();
		$(this).parents('li.default').hide();
	});

	var userId = $('#userid').val();
	
	function save() {
		var block = $(this).parents('li').find('.descripting');
		var addCompany = $(this).parents('li').find('h4 input.company').val();
		var addDescription = block.find('input.description').val();
		var addHours = block.find('input.hours').val();
		var addPhone = block.find('input.phone').val();
		var addSite = block.find('input.site').val();
		var addCity = block.find('select.city').val();
		var addAddress = block.find('input.address').val();
		var addRating = block.find('input.rating').val();
		var addStatus = block.find('input.status').val();
		$('ul.items-list').prepend('<li id="ajax-loader"><img src="/i/ajax-loader-small.gif" /></li>');
		$.ajax({
			url: '/a_ajax/partner/location.save',
			data: {
				company: addCompany,
				description: addDescription,
				hours: addHours,
				phone: addPhone,
				site: addSite,
				city: addCity,
				address: addAddress,
				rating: addRating,
				status: addStatus,
				userid: userId
			},
			dataType: "json",
			type: 'POST',
			success: function(data) {
				if (data.error == 0) {
					var newItem = $('ul.items-list li.example').clone(true);
					newItem.removeClass('example').addClass('view').addClass('new-view');
					newItem.find('h4 a.company').text(addCompany);
					newItem.find('span.description').text(addDescription);
					newItem.find('span.hours').text(addHours);
					newItem.find('span.phone').text(addPhone);
					newItem.find('span.site a').text(addSite).attr('href', 'http://' + addSite);
					newItem.find('span.city').text(block.find('select.city option[value="' + addCity + '"]').text());
					newItem.find('span.address').text(addAddress);
					newItem.find('h4 span.addr').text(addAddress);
					newItem.find('span.rating').text(addRating);
					newItem.find('input.partner_location_id').attr('name', 'partner_location_' + data.plid).val(data.plid);
					if (block.find('input.status').attr('checked') == true) {
						newItem.find('input.status').attr('checked', 'checked');
						newItem.find('input.status').next('a.tm-checkbox').find('img').addClass('checked');
					}
					newItem.insertAfter('.default');
					$('ul.items-list li.new-view').each(function() {
						$(this).find('h4 a.company').bind('click', toggle);
						$(this).find('.ctrl .delete').bind('click', del);
						$(this).find('.ctrl .edit').bind('click', edit);
						$(this).find('.b-ctrl .cancel').bind('click', cancel);
						$(this).find('.b-ctrl .save').bind('click', update);
						$(this).find('.status').bind('click', setStatus);
					});
					$('ul.items-list li.default input.status').removeAttr('checked');
					$('ul.items-list li.default input.status').next('a.tm-checkbox').find('img').removeClass('checked');
					$('ul.items-list li.nofirms').hide();
					$('#add-item').show();
					$('ul.items-list li.default').hide();
				}
				$('#ajax-loader').remove();
			},
			error: function() {
				$('#ajax-loader').remove();
			}
		});
	}
	
	function del() {
		var partnerLocationId = $(this).parents('li').find('input.partner_location_id').val();
		var $this = $(this);
		if (confirm('Вы действительно хотите удалить?')) {
			$.ajax({
				url: '/a_ajax/partner/location.delete?plid=' + partnerLocationId,
				type: 'GET',
				success: function() {
					$this.parents('li').fadeOut('fast', function() {
						$(this).remove();
						if ($('ul.items-list li.view').length <= 0) {
							$('ul.items-list li.nofirms').show();
						}
					});
				}
			});
		}
	}
	
	function edit() {
		var $this = $(this);
		$this.parents('li').find('.type-control').each(function() {
			var val = $(this).siblings('.type-text').text();
			$(this).find('input').val(val);
		});
		var selectVal = $this.parents('li').find('select.city option:contains(' + $this.parents('li').find('span.city').text() + ')').val();
		$this.parents('li').find('.type-control select.city').val(selectVal);
		$this.hide();
		$this.parents('li').find('.descripting').show();
		$this.parents('li').find('.descripting .b-ctrl').show();
		$this.parents('li').find('.type-text').hide();
		$this.parents('li').find('.type-control').show();
		$this.parents('li').find('.addr').hide();
	}

	function cancel() {
		$(this).parents('.b-ctrl').hide();
		$(this).parents('.descripting').hide();
		$(this).parents('li').find('.ctrl .edit').show();
		$(this).parents('li').find('.type-text').show();
		$(this).parents('li').find('.type-control').hide();
		$(this).parents('li').find('.addr').show();
	}

	function update() {
		var $this = $(this);
		var block = $this.parents('li').find('.descripting');
		var addCompany = $this.parents('li').find('h4 input.company').val();
		var addDescription = block.find('input.description').val();
		var addHours = block.find('input.hours').val();
		var addPhone = block.find('input.phone').val();
		var addSite = block.find('input.site').val();
		var addCity = block.find('select.city').val();
		var addAddress = block.find('input.address').val();
		var addRating = block.find('input.rating').val();
		var addStatus = block.find('input.status').val();
		var addPlid = $this.parents('li').find('input.partner_location_id').val();
		$.ajax({
			url: '/a_ajax/partner/location.update',
			data: {
				company: addCompany,
				description: addDescription,
				hours: addHours,
				phone: addPhone,
				site: addSite,
				city: addCity,
				address: addAddress,
				rating: addRating,
				userid: userId,
				plid: addPlid
			},
			dataType : "json",
			type: 'POST',
			success: function(data) {
				if (data.error == 0) {
					$this.parents('li').find('h4 a.company').text(addCompany);
					$this.parents('li').find('.descripting span.description').text(addDescription);
					$this.parents('li').find('.descripting span.hours').text(addHours);
					$this.parents('li').find('.descripting span.phone').text(addPhone);
					$this.parents('li').find('.descripting span.site a').text(addSite).attr('href', 'http://' + addSite);
					$this.parents('li').find('.descripting span.city').text(block.find('select.city option[value="' + addCity + '"]').text());
					$this.parents('li').find('.descripting span.address').text(addAddress);
					$this.parents('li').find('.descripting span.rating').text(addRating);
					$this.parents('li').find('h4 span.addr').text(addAddress);
					
					$this.parents('.b-ctrl').hide();
					//$(this).parents('.descripting').hide();
					$this.parents('li').find('.ctrl .edit').show();
					$this.parents('li').find('.type-text').show();
					$this.parents('li').find('.type-control').hide();
					$this.parents('li').find('.addr').show();
				}
			}
		});
	}

	function setStatus() {
		var partnerLocationId, status;
		if ($(this).parents('li').hasClass('view')) {
			partnerLocationId = $(this).parents('li').find('input.partner_location_id').val();
			status = $(this).attr('checked');
			$.ajax({
				url: '/a_ajax/partner/location.changeStatus?plid=' + partnerLocationId + '&status=' + status,
				type: 'GET'
			});
		}
	}
	
	$('.default .b-ctrl .save').bind('click', save);
	$('.view .ctrl .delete').bind('click', del);
	$('.view .b-ctrl .cancel').bind('click', cancel);
	$('.view .b-ctrl .save').bind('click', update);
	$('.view .ctrl .edit').bind('click', edit);
	$('.status').bind('click', setStatus);
	
});